#include "stdafx.h"
#include "Stock1.h"
#include <iostream>

Stock1::Stock1(void)
{
	
	std::cout <<"Default constructor called" <<std::endl;
	strcpy(company,"no name");
	shares = 0;
	share_val=0.0;
	total_val = 0.0;
}

Stock1::Stock1(const char * co,int n,double pr){
	std::cout <<"Constructor using " <<co <<" called" <<std::endl;
	strncpy(company,co,29);
	company[29] = '\0';
	if(n <0){
		std::cerr <<"Number of shares can't be negative."
			<< company <<" shares set to 0." << std::endl;
		shares =0;
	}
	else{
		shares = n;
	}
	share_val = pr;
	set_tot();
}


Stock1::~Stock1(void)
{
	std::cout <<"Bye bye"<<std::endl;
}

void Stock1::buy(int num,double price){
	if(num <0){
		std::cerr <<"Number of shares can't be negative." <<"Transaction is aborted." <<std::endl;
	}
	else
	{
		shares += num;
		share_val = price;
		set_tot();
	}
};

void Stock1::sell(int num,double price){
	if(num <0){
		std::cerr <<"Number of shares can't be negative." <<"Transaction is aborted." <<std::endl;
	}
	else
	{
		shares -= num;
		share_val = price;
		set_tot();
	}
};

void Stock1::update(double price){
	share_val = price;
	set_tot();
};

void Stock1::show(){
	std::cout <<"Company: " << company 
		<<" shares:" <<shares <<std::endl
	<<" share price: $" <<share_val
	<<" Total Worth: $" << total_val <<std::endl;
};